package xyz.tanxiao.links.common.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 统一响应结果
 *
 * @author 万成波
 * @email 2300064869@qq.com
 * @createTime 2023-06-08 21:36:55 星期四
 */
@Getter
@Setter
public class ResponseResult implements Serializable {

	private static final long serialVersionUID = 1193332745273179357L;

	/**
	 * 响应状态码
	 */
	private int code;

	/**
	 * 响应消息
	 */
	private String message;

	/**
	 * 响应数据
	 */
	private Object data;

	/**
	 * 响应日期时间 (yyyy-MM-dd HH:mm:ss)
	 */
	private String timestamp;

	/**
	 * 响应日期时间格式
	 */
	private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


	@Getter
	@AllArgsConstructor
	public enum ResultEnum {

		SUCCESS(200, "操作成功"),

		PARAMS_ERROR(400, "操作失败，请求参数错误。"),

		SYSTEM_ERROR(500, "操作失败，系统错误。");

		private final Integer code;

		private final String message;

	}


	private ResponseResult(int code, String message, Object data) {
		this.code = code;
		this.message = message;
		this.data = data;
		this.timestamp = LocalDateTime.now().format(DATE_TIME_FORMATTER);
	}


	/**
	 * 成功消息
	 *
	 * @return ResponseMessage 实例
	 */
	public static ResponseResult success() {
		return new ResponseResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), null);
	}


	/**
	 * 错误消息
	 *
	 * @return ResponseMessage 实例
	 */
	public static ResponseResult error() {
		return new ResponseResult(ResultEnum.SYSTEM_ERROR.getCode(), ResultEnum.SYSTEM_ERROR.getMessage(), null);
	}


	/**
	 * 错误消息
	 *
	 * @return ResponseMessage 实例
	 */
	public static ResponseResult result(ResultEnum result) {
		return new ResponseResult(result.getCode(), result.getMessage(), null);
	}


	/**
	 * 设置状态码
	 *
	 * @param code 状态码
	 * @return 当前 ResponseMessage 对象
	 */
	public ResponseResult code(int code) {
		this.code = code;
		return this;
	}


	/**
	 * 设置响应消息
	 *
	 * @param message 响应消息
	 * @return 当前 ResponseMessage 对象
	 */
	public ResponseResult message(String message) {
		this.message = message;
		return this;
	}


	/**
	 * 设置响应数据
	 *
	 * @param data 响应数据
	 * @return 当前 ResponseMessage 对象
	 */
	public ResponseResult data(Object data) {
		this.data = data;
		return this;
	}

}
